<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">昵称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="nickname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">品牌</label>
                            <div class="layui-input-block">
                                <select name="brandType" id="brandType">
                                    <option value=>全部</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">开始日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="startTime" id="startTime" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">结束日期</label>
                            <div class="layui-input-block">
                                <input type="text" name="endTime" id="endTime" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>


        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add">添加记录</button>
            </div>
        </script>

        <script type="text/html" id="recTableBar">
            <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            roleType = layui.data("user").roleType,
            table = layui.table;

        laydate.render({
            elem: '#startTime',
            type: 'datetime'
        });
        laydate.render({
            elem: '#endTime',
            type: 'datetime'
        });

        $.ajax({
            type: "get",
            url: "/zxjy/enum/brandtype",
            // url: '../api/status.json',
            headers: {
                'Authorization': layui.data("Authorization")['Authorization'],
            },
            success: function (result) {
                if (result.state === 200) {
                    for (obj of result.result) {
                        $("#brandType").append("<option value=\""+obj.key+"\">"+obj.value+"</option>");
                    }
                    //重新渲染
                    layui.form.render("select");
                }
            }
        });

        var cols = [[
            {field: 'nickname', title: '昵称'},
            {field: 'xueersiCount', title: '学而思'},
            {field: 'gaotu100Count', title: '高途'},
            {field: 'zybangCount', title: '作业帮'},
            {field: 'yuanfudaoCount', title: '猿辅导'},
            {field: 'count', title: '完成总下单数量'},
            {field: 'completeDate', title: '员工完成下单日期'}
        ]]

        if (roleType === "RECORDER"){
            cols[0].push({title: '操作',toolbar: '#recTableBar', align: "center"});
        }

        table.render({
            elem: '#currentTableId',
            // url: '../api/user.json',
            url: '/zxjy/statistics/list/' + roleType.toLowerCase(),
            toolbar: roleType === "RECORDER" ? '#toolbarDemo' : '',
            method: 'post',
            contentType: 'application/json',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cellMinWidth: 100,
            cols: cols,
            response: {statusCode: 200},
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": res.state, //解析接口状态
                    "msg": res.success, //解析提示文本
                    "count": res.result.total, //解析数据长度
                    "data": res.result.list //解析数据列表
                };
            },
            request: {
                pageName: 'pageNum' // 页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            //执行搜索重载
            table.reload('currentTableId', {
                url: '/zxjy/statistics/list/' + roleType.toLowerCase()
                , where: {
                    nickname: data.field.nickname,
                    brandType: data.field.brandType === ''?null:data.field.brandType,
                    startTime: data.field.startTime,
                    endTime: data.field.endTime,
                } //设定异步数据接口的额外参数
            });


            //执行搜索重载
            // table.reload('currentTableId', {
            //     page: {
            //         curr: 1
            //     }
            //     , where: {
            //         wechatNo: result.wechatNo
            //     }
            // }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {
                layer.open({
                    title: '新增',
                    type: 2,
                    shadeClose: true,
                    offset: '150px',
                    area: ['550px', '500px'],
                    content: '../page/table/addstatistics.html'
                });
            }
        });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                if(obj.event === 'edit') {
                layer.open({
                    title: '修改',
                    type: 2,
                    shadeClose: true,
                    offset: '150px',
                    area: ['550px', '500px'],
                    content: '../page/table/editstatistics.html',
                    success: function(layero, index) {
                        $("#id", layero.find("iframe")[0].contentWindow.document).attr("value",  data.id );
                        $("#fBrandType", layero.find("iframe")[0].contentWindow.document).attr("value",data.brandType);
                        $("#nickname", layero.find("iframe")[0].contentWindow.document).attr("value",data.nickname);
                        $("#count", layero.find("iframe")[0].contentWindow.document).attr("value",data.count);
                    }
                });
            }
        });

    });
</script>

</body>
</html>
